package com.yhny.lykj.yuhannongye.ui;

import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.longsichao.lscframe.view.LSCImageView;
import com.yhny.lykj.yuhannongye.R;
import com.yhny.lykj.yuhannongye.app.BaseFragment;
import com.yhny.lykj.yuhannongye.app.UserConfig;

/**
 * Created by leach on 2017/7/26.
 */

public class UserFragment extends BaseFragment {
    private TextView leftView;
    TextView nameView;
    private LSCImageView photoView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_user, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        TextView textView = (TextView)view.findViewById(R.id.name_top_bar);
        textView.setText("会员中心");
        view.findViewById(R.id.left_group).setOnClickListener(this);
        leftView = (TextView)view.findViewById(R.id.left_text);
        leftView.setOnClickListener(this);
        leftView.setVisibility(View.GONE);
        nameView = (TextView)view.findViewById(R.id.user_name);
        nameView.setOnClickListener(this);
        photoView = (LSCImageView)view.findViewById(R.id.user_photo);
        photoView.setOnClickListener(this);
        view.findViewById(R.id.user_all_order).setOnClickListener(this);
        view.findViewById(R.id.user_address).setOnClickListener(this);
        view.findViewById(R.id.user_order_pay).setOnClickListener(this);
        view.findViewById(R.id.user_order_address).setOnClickListener(this);
        view.findViewById(R.id.outin).setOnClickListener(this);
        view.findViewById(R.id.user_yue).setOnClickListener(this);
        initData();
    }
    private void initData(){
        if (TextUtils.isEmpty(UserConfig.getConfigUserId())){
            nameView.setText("登录/注册");//用户姓名
        }else{
            nameView.setText(UserConfig.getConfigUserName());//用户姓名
            //用户头像
            Resources resources = getContext().getResources();
            int height = resources.getDimensionPixelOffset(R.dimen.item_shoper_height);
            int width = resources.getDimensionPixelOffset(R.dimen.item_shoper_height);
            String imageUrl = "";
            if (UserConfig.getConfigUserPhotoUrl()!=null&&!TextUtils.isEmpty(UserConfig.getConfigUserPhotoUrl())) {
                imageUrl = UserConfig.getConfigUserPhotoUrl();
            }
            if (!TextUtils.isEmpty(imageUrl)) {
                photoView.setType(LSCImageView.Type.CropCircle).setPlaceHolder(R.drawable.default_user).setTargetSize(width, height).setImageURI(Uri.parse(imageUrl));
            }else{
                photoView.setType(LSCImageView.Type.CropCircle).setImageResource(R.drawable.default_user);
            }
        }

    }

    @Override
    public void onResume() {
        initData();
        super.onResume();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.outin:{
                //退出登录
                Intent intent = new Intent(getActivity(),LoginActivity.class);
                startActivity(intent);
                //退出登录
                UserConfig.clearUser();
                break;
            }
            case R.id.user_yue:{
                //关于我们
                Intent intent = new Intent(getActivity(),AboutActivity.class);
                startActivity(intent);

                break;
            }
            case R.id.user_address:{
                //管理收货地址
                Intent intent = new Intent(getActivity(),AddressListActivity.class);
                startActivity(intent);

                break;
            }
            case R.id.user_photo:{
                //个人资料
                Intent intent = new Intent(getActivity(),UserInfoActivity.class);
                startActivity(intent);

                break;
            }
            case R.id.user_name:{
                if (TextUtils.isEmpty(UserConfig.getConfigUserId())){
                    Intent intent  = new Intent(getActivity(),LoginActivity.class);
                    startActivity(intent);
                }
                break;
            }
            case R.id.user_all_order:{
                //全部订单
                Intent intent = new Intent(getActivity(),MyOrderListActivity.class);
                intent.putExtra(ORDER_FROM,ORDER_ALL);
                startActivity(intent);
                break;
            }
            case R.id.user_order_pay:{
                //待支付订单
                Intent intent = new Intent(getActivity(),MyOrderListActivity.class);
                intent.putExtra(ORDER_FROM,ORDER_PAY);
                startActivity(intent);
                break;
            }
            case R.id.user_order_post:{
                //待收货
                Intent intent = new Intent(getActivity(),MyOrderListActivity.class);
                intent.putExtra(ORDER_FROM,ORDER_POST);
                startActivity(intent);
                break;
            }
            case R.id.user_order_address:{
                //待收货
                Intent intent = new Intent(getActivity(),MyOrderListActivity.class);
                intent.putExtra(ORDER_FROM,ORDER_ADDRESS);
                startActivity(intent);
                break;
            }

        }
    }
}
